<?php
require_once "BaseAction.php";
require_once 'lib/thrift/client/MusicService.php';

class Event_MusicController extends TTEvent_Base_Action 
{
	
	public function init(){
		parent::init();
		$this->_helper->layout->setLayout('event-detail');
		$this->_helper->eventCommon->buildMainMenuBlock();
	}
	
	// music list
	public function indexAction(){
		$eid = (int)$this->_getParam('eid', 0);
		if(!$eid)
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		
		$event = $this->_helper->modelLoader->getEventInfo($eid);
		if(empty($event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		}
		if(!$this->_helper->privacy->isEnable('music', $event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event/detail/'.$eid);
		}
		
        $this->setTitle("Nhạc | ".$event->event_name);
		$user = $this->getUser();
		$req = $this->getRequest();
		
		
		if ($req->isPost())
		{
			$music_delete = $req->getPost('music_delete', 0);
			$sids = $req->getPost('sids', array());
			if ($music_delete==1)
			{
				if(!empty($sids)){
					$this->delEventMusics($eid, $sids);
					$changed = array('changed'=>time(), 'music_count'=>$this->getEventMusicCount($eid));
					$this->_helper->modelLoader->changeEventInfo($eid, $changed);
					$this->view->removeSuccess = 1;
					$this->view->removeCount = count($sids);
				}
			}
		}
		$page = $req->getQuery('page', 0);
		$limit = 20;
		
		$total = $this->getEventMusicCount($eid);
		$totalPage = ceil($total/$limit);
		if($page >= $totalPage)
			$page = $totalPage-1;
		$eventMusics = $this->getEventMusics($eid, $page+1, $limit);
		
		$this->view->musicList = $eventMusics;
		$this->view->totalMusic = $total;
		$this->view->limit = $limit;
		$this->view->uid = $user->uid;
		//$this->view->pager = $this->view->partial("pager-new.phtml", array('page'=>$page, "url" => "/event/music/" . $event->eid, "param" => "", 'haveNext'=>(count($eventMusics['list'])>$limit)));
		
		$pageInfo = array();
		$pageInfo['total_item'] = $total;
		$pageInfo['page'] = $page;
		$pageInfo['total_page'] = $totalPage;
		$this->view->pager = $this->view->partial("pager.phtml", array('pageInfo'=>$pageInfo, 'ext_param'=>"", "url" => "/event/music/" . $event->eid, "param" => "", 'title'=>'nhạc'));
		$this->view->event = $event;
		$this->view->page = $page;
		
		$this->view->isAdmin  = $this->_helper->modelLoader->isEventAdmin($eid, $user->uid);
		$this->view->action = 'list';
		$this->view->htmlLeftCol = $this->_helper->eventCommon->buildHtmlEventLeftCol($eid);
		
	}
	
	public function delEventMusics($eid, $sids){
		$eid = (int)$eid;
		if($eid && is_array($sids) && count($sids)>0){
			$success = false;
			try {
				$v_client = new MusicService(MUSIC_THRIFT_SERVER, MUSIC_THRIFT_PORT);
				$v_client->OpenTransport();
				$v_client->client()->removeEventMusics($eid, $sids);
				$v_client->CloseTransport();
				$success = true;
			} catch (Exception $ex){
				//$musics = array();
				$success = false;
			}
			if($success){
				$this->clearEventMusicCache($eid);
			}
		}
	}
	
	public function addEventMusics($eid, $sids, $uid){
		
		$eid = (int)$eid;
		if($eid && is_array($sids) && count($sids)>0){
			$success = false;
			try {
				$v_client = new MusicService(MUSIC_THRIFT_SERVER, MUSIC_THRIFT_PORT);
				$v_client->OpenTransport();
				$v_client->client()->addEventMusics($eid, $sids, $uid);
				$v_client->CloseTransport();
				$success = true;
			} catch (Exception $ex){
				$success = false;
				//$musics = array();
			}
			if($success){
				$this->clearEventMusicCache($eid);
			}
		}
	}
	
	
	public function addAction(){
		$eid = (int)$this->_getParam('eid', 0);
		if(!$eid)
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		
		$event = $this->_helper->modelLoader->getEventInfo($eid);
		if(empty($event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		}
        
        $this->setTitle("Thêm nhạc | ".$event->event_name);
		$user = $this->getUser();
		if(!$this->_helper->privacy->isCanAdd('music', $eid, $user->uid)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event/detail/'.$eid);
		}
		if(!$this->_helper->privacy->isEnable('music', $event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event/detail/'.$eid);
		}
		$req = $this->getRequest();
		
		
		if ($req->isPost())
		{
			$sids = $req->getPost('sids', array());
			$music_add = $req->getPost('music_add', 0);
			$sids = $req->getPost('sids', array());
			
			if ($music_add==1)
			{
				if(!empty($sids)){
					$this->addEventMusics($eid, $sids, $user->uid);
					$changed = array('changed'=>time(), 'music_count'=>$this->getEventMusicCount($eid));
					$this->_helper->modelLoader->changeEventInfo($eid, $changed);
					$this->view->addSuccess = 1;
					$this->view->addCount = count($sids);
				}
			}
		}
		$page = $req->getQuery('page', 0);
		$limit = 20;
		
		$total = $this->getUserMusicCount($user->uid);
		$eventMusics = $this->getEventUserMusic($user->uid, $eid, $page+1, $limit);
		
		$this->view->musicList = $eventMusics;
		$this->view->limit = $limit;
		$this->view->uid = $user->uid;
		//$this->view->pager = $this->view->partial("pager-new.phtml", array('page'=>$page, "url" => "/event/music/add/" . $event->eid, "param" => "", 'haveNext'=>(count($eventMusics)>$limit)));
		
		$pageInfo = array();
		$pageInfo['total_item'] = $total;
		$pageInfo['page'] = $page;
		$pageInfo['total_page'] = ceil($total/$limit);
		
		$this->view->pager = $this->view->partial("pager.phtml", array('pageInfo'=>$pageInfo, 'ext_param'=>"", "url" => "/event/music/add/" . $event->eid, "param" => "", 'title'=>'nhạc'));
		
		$this->view->event = $event;
		$this->view->page = $page;
		
		$this->view->isAdmin  = $this->_helper->modelLoader->isEventAdmin($eid, $user->uid);
		$this->view->action = 'list';
		$this->view->htmlLeftCol = $this->_helper->eventCommon->buildHtmlEventLeftCol($eid);
	}
	
	public function getEventMusicCount($eid){
		$v_client = null;
		$total = 0;
		
		try {
			$v_client = new MusicService(MUSIC_THRIFT_SERVER, MUSIC_THRIFT_PORT);
			$v_client->OpenTransport();
			$total = $v_client->client()->getEventMusicCount($eid);
			$v_client->CloseTransport();
		} catch (Exception $ex){
			//$musics = array();
		}
		return $total;
	}
	
	public function getUserMusicCount($eid){
		$v_client = null;
		$total = 0;
		
		try {
			$v_client = new MusicService(MUSIC_THRIFT_SERVER, MUSIC_THRIFT_PORT);
			$v_client->OpenTransport();
			$total = $v_client->client()->getUserMusicCount($eid);
			$v_client->CloseTransport();
		} catch (Exception $ex){
			//$musics = array();
		}
		return $total;
	}
	
	public function getEventMusics($eid, $page=1, $limit=20){
		
		$v_client = null;
		$eventMusics = array();
		$page = (int)$page;
		if($page < 1) $page = 1;
		
		try {
			$v_client = new MusicService(MUSIC_THRIFT_SERVER, MUSIC_THRIFT_PORT);
			$v_client->OpenTransport();
			$eventMusics = $v_client->client()->getEventMusic($eid, $page, $limit);
			$v_client->CloseTransport();
		} catch (Exception $ex){
			//$musics = array();
		}
		return $eventMusics;
	}
	
	public function getEventUserMusic($uid, $eid, $page, $limit){
		$v_client = null;
		$eventMusics = array();
		
		try {
			$v_client = new MusicService(MUSIC_THRIFT_SERVER, MUSIC_THRIFT_PORT);
			$v_client->OpenTransport();
			$eventMusics = $v_client->client()->getUserEventMusic($uid, $eid, $page, $limit);
			$v_client->CloseTransport();
		} catch (Exception $ex){
			//$musics = array();
		}
		return $eventMusics;
		
	}
	
	private function clearEventMusicCache($eid){
		$this->deleteCache("html_event_music_block_".$eid);
	}
}

